//
//  NewBookVC.m
//  sweetbox
//
//  Created by apollo on 15/9/5.
//  Copyright (c) 2015年 proj-m. All rights reserved.
//

#import "NewBookVC.h"
#import "log.h"
#import "DBHelper.h"

@interface NewBookVC ()

@property (weak, nonatomic) IBOutlet UITextField *nameTF;

@property (weak, nonatomic) IBOutlet UITextField *briefTF;
@end

@implementation NewBookVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)onCancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^(){
    
    }];
}
- (IBAction)onNameDone:(id)sender {
    [sender resignFirstResponder];
}

- (IBAction)onBriefDone:(id)sender {
    [sender resignFirstResponder];
}

- (IBAction)onDone:(id)sender {
    NSString *name = self.nameTF.text;
    NSString *brief = self.briefTF.text;
    
    if([name isEqualToString:@""] || [brief isEqualToString:@""]) {
        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写完成" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        return ;
    }
    NSLog(@"name :%@",name);
    NSLog(@"brief :%@",brief);
    
    BOOL ret = [[DBHelper sharedDBHelper] insertBook:name brief:brief];
    if (YES == ret) {
        LOG_I("Save book %s Success",[name cStringUsingEncoding:NSUTF8StringEncoding] );
        
    } else {
        LOG_E("Save book %s Failed",[name cStringUsingEncoding:NSUTF8StringEncoding] );
    }
    
    
    [self dismissViewControllerAnimated:YES completion:^(){
        [[NSNotificationCenter defaultCenter] postNotificationName:@"save_book" object:self];
    }];
}

@end
